package com.shenyue.nbtownlet.model;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.util.StorageUtil;
import com.shenyue.nbtownlet.util.StringUtil;

/**
 * 项目管理
 */

public class AppProjectModel {
    private static final String ROOT = "SmallTown/XCZProjects";
    private static final String ROOT_DATA = "$smalltown_xczprojects_data$";
    /***获取项目列表接口****/
    public static final String GETPROJECTS = "GetProjects";
    public static final String GETPROJECTS_ACTION = "$getprojects_action$";

    /***获取项目类别****/
    public static final String GETPROJECTTYPE = "GetProjectType";
    public static final String GETPROJECTTYPE_ACTION = "$getprojecttype_action$";

    /***获取项目详情接口****/
    public static final String GETPROJECTDETAILS = "GetProjectDetails";
    public static final String GETPROJECTDETAILS_ACTION = "$getprojectdetails_action$";


    public static void GetProjects(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().get(AppModel.kBaseURL + "/" + ROOT + "/" + GETPROJECTS, GETPROJECTS_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void GetProjectType(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().get(AppModel.kBaseURL + "/" + ROOT + "/" + GETPROJECTTYPE, GETPROJECTTYPE_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                addDATA(context, action, val);
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    public static void GetProjectDetails(final Context context, JSONObject param, final BaseListener listener) {
        HttpClientExManager.instance().get(AppModel.kBaseURL + "/" + ROOT + "/" + GETPROJECTDETAILS, GETPROJECTDETAILS_ACTION, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                if (listener != null)
                    listener.success(val, action);
            }

            @Override
            public void error(String error, String action) {
                if (listener != null)
                    listener.error(error, action);
            }
        });
    }

    private synchronized static void addDATA(Context context, String action, String val) {
        String data = StorageUtil.getLocalStorage(context, ROOT_DATA);
        JSONObject $data;
        if (StringUtil.isNull(data))
            $data = new JSONObject();
        else
            $data = JSON.parseObject(data);
        $data.put(action, val);
        StorageUtil.setLocalStorage(context, ROOT_DATA, $data.toString());
    }

    public static JSONArray getProjectType(Context context){
        try {
            String data = StorageUtil.getLocalStorage(context, ROOT_DATA);
            if (StringUtil.isNull(data))
                return null;
            JSONObject $data = JSON.parseObject(data);
            return JSON.parseArray($data.getString(GETPROJECTTYPE_ACTION));
        }catch (Exception e){
            return null;
        }
    }
}
